/*
  SE05X Certificate

  This sketch uses the SE05X to store device certificate and read it back.

  Circuit:
   - Portenta C33
*/

#include <SE05X.h>

const byte certificate[410] = {
  0x30 ,0x82 ,0x01 ,0x96 ,0x30 ,0x82 ,0x01 ,0x3D ,0xA0 ,0x03 ,0x02 ,0x01 ,0x02 ,0x02 ,0x10 ,0x37,
  0xFE ,0x48 ,0x92 ,0xE6 ,0xC0 ,0xA0 ,0x64 ,0x68 ,0x91 ,0x66 ,0x5F ,0x7D ,0xE3 ,0x02 ,0xDE ,0x30,
  0x0A ,0x06 ,0x08 ,0x2A ,0x86 ,0x48 ,0xCE ,0x3D ,0x04 ,0x03 ,0x02 ,0x30 ,0x45 ,0x31 ,0x0B ,0x30,
  0x09 ,0x06 ,0x03 ,0x55 ,0x04 ,0x06 ,0x13 ,0x02 ,0x55 ,0x53 ,0x31 ,0x17 ,0x30 ,0x15 ,0x06 ,0x03,
  0x55 ,0x04 ,0x0A ,0x13 ,0x0E ,0x41 ,0x72 ,0x64 ,0x75 ,0x69 ,0x6E ,0x6F ,0x20 ,0x4C ,0x4C ,0x43,
  0x20 ,0x55 ,0x53 ,0x31 ,0x0B ,0x30 ,0x09 ,0x06 ,0x03 ,0x55 ,0x04 ,0x0B ,0x13 ,0x02 ,0x49 ,0x54,
  0x31 ,0x10 ,0x30 ,0x0E ,0x06 ,0x03 ,0x55 ,0x04 ,0x03 ,0x13 ,0x07 ,0x41 ,0x72 ,0x64 ,0x75 ,0x69,
  0x6E ,0x6F ,0x30 ,0x20 ,0x17 ,0x0D ,0x32 ,0x33 ,0x30 ,0x33 ,0x33 ,0x31 ,0x30 ,0x37 ,0x30 ,0x30,
  0x30 ,0x30 ,0x5A ,0x18 ,0x0F ,0x32 ,0x30 ,0x35 ,0x34 ,0x30 ,0x33 ,0x33 ,0x31 ,0x30 ,0x37 ,0x30,
  0x30 ,0x30 ,0x30 ,0x5A ,0x30 ,0x2F ,0x31 ,0x2D ,0x30 ,0x2B ,0x06 ,0x03 ,0x55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0xFC ,0xE1 ,0xB9 ,0x80 ,0x30 ,0x0A ,0x06 ,0x08 ,0x2A ,0x86 ,0x48 ,0xCE ,0x3D ,0x04 ,0x03,
  0x02 ,0x03 ,0x47 ,0x00 ,0x30 ,0x44 ,0x02 ,0x20 ,0x16 ,0x85 ,0x8A ,0x58 ,0x07 ,0x28 ,0xEF ,0x6D,
  0x93 ,0x86 ,0xA0 ,0x0E ,0xC8 ,0xB0 ,0x0A ,0xAD ,0x3B ,0xCE ,0xBB ,0x6A ,0x19 ,0x94 ,0xF9 ,0xD3,
  0x05 ,0x2E ,0x15 ,0xF1 ,0x5E ,0x9F ,0x59 ,0xD2 ,0x02 ,0x20 ,0x45 ,0x30 ,0x88 ,0x1D ,0x24 ,0xDA,
  0xE4 ,0x60 ,0xE2 ,0xD0 ,0x6E ,0x02 ,0xB0 ,0x7D ,0x65 ,0xA8 ,0x09 ,0x63 ,0x0B ,0x44 ,0xBC ,0x24,
  0x1A ,0xE2 ,0xEC ,0x64 ,0x19 ,0xB4 ,0x59 ,0xB8 ,0x09 ,0x78
};

void printBufferHex(const byte input[], size_t inputLength) {
    Serial.println(inputLength);
  for (size_t i = 0; i < inputLength; i++) {
    Serial.print(input[i] >> 4, HEX);
    Serial.print(input[i] & 0x0f, HEX);
  }
  Serial.println();
}

void setup() {
  Serial.begin(9600);
  while (!Serial);

  if (!SE05X.begin()) {
    Serial.println("Failed to communicate with SE05X!");
    while (1);
  }

  const int certId = 799;

  if (SE05X.existsBinaryObject(certId)) {
    Serial.println("Object already present, deleting...");
    SE05X.deleteBinaryObject(certId);
  }

  if (SE05X.writeBinaryObject(certId, certificate, sizeof(certificate))) {
    Serial.println("Certificate stored into SE050");
  } else {
    Serial.println("Failed to store certificate");
    return;
  }

  byte buf[512];
  size_t len;

  if (SE05X.readBinaryObject(certId, buf, sizeof(buf), &len)) {
    Serial.print("Certificate is:                  ");
    printBufferHex(buf, len);
  } else {
    Serial.println("Failed to read certificate");
    return;
  }

  /* Rewrite some random data using alternative APIs */
  if(SE05X.random(buf, sizeof(buf))) {
    Serial.print("New data is:                     ");
    printBufferHex(buf, sizeof(buf));
  }

  if(SE05X.writeSlot(certId, buf, sizeof(buf))) {
    Serial.println("Data stored into SE050");
  } else {
    Serial.println("Failed to store data");
    return;
  }

  if(SE05X.readSlot(certId, buf, sizeof(buf))) {
    Serial.print("Readback data is:                ");
    printBufferHex(buf, sizeof(buf));
  } else {
    Serial.println("Failed to read data");
    return;
  }

}

void loop() {

}
